
Imports std

Namespace language
    Namespace japanese

        Public Module ___hira2pron___

            Public ReadOnly repeat() As Char = {hextochar("0x309d"), hextochar("0x309e")}

            Public ReadOnly pron(,) As String = { _
                            {hextochar("0x3041"), shortenformat("la")}, _
                            {hextochar("0x3042"), shortenformat("a")}, _
                            {hextochar("0x3043"), shortenformat("li")}, _
                            {hextochar("0x3044"), shortenformat("i")}, _
                            {hextochar("0x3045"), shortenformat("lu")}, _
                            {hextochar("0x3046"), shortenformat("u")}, _
                            {hextochar("0x3047"), shortenformat("le")}, _
                            {hextochar("0x3048"), shortenformat("e")}, _
                            {hextochar("0x3049"), shortenformat("lo")}, _
                            {hextochar("0x304a"), shortenformat("o")}, _
                            {hextochar("0x304b"), shortenformat("ka")}, _
                            {hextochar("0x304c"), shortenformat("ga")}, _
                            {hextochar("0x304d"), shortenformat("ki")}, _
                            {hextochar("0x304e"), shortenformat("gi")}, _
                            {hextochar("0x304f"), shortenformat("ku")}, _
                            {hextochar("0x3050"), shortenformat("gu")}, _
                            {hextochar("0x3051"), shortenformat("ke")}, _
                            {hextochar("0x3052"), shortenformat("ge")}, _
                            {hextochar("0x3053"), shortenformat("ko")}, _
                            {hextochar("0x3054"), shortenformat("go")}, _
                            {hextochar("0x3055"), shortenformat("sa")}, _
                            {hextochar("0x3056"), shortenformat("za")}, _
                            {hextochar("0x3057"), shortenformat("si")}, _
                            {hextochar("0x3058"), shortenformat("zi")}, _
                            {hextochar("0x3059"), shortenformat("su")}, _
                            {hextochar("0x305a"), shortenformat("zu")}, _
                            {hextochar("0x305b"), shortenformat("se")}, _
                            {hextochar("0x305c"), shortenformat("ze")}, _
                            {hextochar("0x305d"), shortenformat("so")}, _
                            {hextochar("0x305e"), shortenformat("zo")}, _
                            {hextochar("0x305f"), shortenformat("ta")}, _
                            {hextochar("0x3060"), shortenformat("da")}, _
                            {hextochar("0x3061"), shortenformat("ti")}, _
                            {hextochar("0x3062"), shortenformat("di")}, _
                            {hextochar("0x3063"), shortenformat("ltu")}, _
                            {hextochar("0x3064"), shortenformat("tu")}, _
                            {hextochar("0x3065"), shortenformat("du")}, _
                            {hextochar("0x3066"), shortenformat("te")}, _
                            {hextochar("0x3067"), shortenformat("de")}, _
                            {hextochar("0x3068"), shortenformat("to")}, _
                            {hextochar("0x3069"), shortenformat("do")}, _
                            {hextochar("0x306a"), shortenformat("na")}, _
                            {hextochar("0x306b"), shortenformat("ni")}, _
                            {hextochar("0x306c"), shortenformat("nu")}, _
                            {hextochar("0x306d"), shortenformat("ne")}, _
                            {hextochar("0x306e"), shortenformat("no")}, _
                            {hextochar("0x306f"), shortenformat("ha")}, _
                            {hextochar("0x3070"), shortenformat("ba")}, _
                            {hextochar("0x3071"), shortenformat("pa")}, _
                            {hextochar("0x3072"), shortenformat("hi")}, _
                            {hextochar("0x3073"), shortenformat("bi")}, _
                            {hextochar("0x3074"), shortenformat("pi")}, _
                            {hextochar("0x3075"), shortenformat("hu")}, _
                            {hextochar("0x3076"), shortenformat("bu")}, _
                            {hextochar("0x3077"), shortenformat("pu")}, _
                            {hextochar("0x3078"), shortenformat("he")}, _
                            {hextochar("0x3079"), shortenformat("be")}, _
                            {hextochar("0x307a"), shortenformat("pe")}, _
                            {hextochar("0x307b"), shortenformat("ho")}, _
                            {hextochar("0x307c"), shortenformat("bo")}, _
                            {hextochar("0x307d"), shortenformat("po")}, _
                            {hextochar("0x307e"), shortenformat("ma")}, _
                            {hextochar("0x307f"), shortenformat("mi")}, _
                            {hextochar("0x3080"), shortenformat("mu")}, _
                            {hextochar("0x3081"), shortenformat("me")}, _
                            {hextochar("0x3082"), shortenformat("mo")}, _
                            {hextochar("0x3083"), shortenformat("lya")}, _
                            {hextochar("0x3084"), shortenformat("ya")}, _
                            {hextochar("0x3085"), shortenformat("lyu")}, _
                            {hextochar("0x3086"), shortenformat("yu")}, _
                            {hextochar("0x3087"), shortenformat("lyo")}, _
                            {hextochar("0x3088"), shortenformat("yo")}, _
                            {hextochar("0x3089"), shortenformat("ra")}, _
                            {hextochar("0x308a"), shortenformat("ri")}, _
                            {hextochar("0x308b"), shortenformat("ru")}, _
                            {hextochar("0x308c"), shortenformat("re")}, _
                            {hextochar("0x308d"), shortenformat("ro")}, _
                            {hextochar("0x308e"), shortenformat("lwa")}, _
                            {hextochar("0x308f"), shortenformat("wa")}, _
                            {hextochar("0x3090"), shortenformat("wi")}, _
                            {hextochar("0x3091"), shortenformat("we")}, _
                            {hextochar("0x3092"), shortenformat("o")}, _
                            {hextochar("0x3093"), shortenformat("nn")}, _
                            {hextochar("0x3094"), shortenformat("bu")}}

            Friend Sub prepare(ByRef input As String)
                Dim index As Int32
                Dim i As Int32
                Dim len As Int32 = strlen(input)
                For i = 0 To repeat.Length() - 1
                    index = input.IndexOf(repeat(i))
                    While index < len And index > 0
                        strrplc(input, index, input(index - 1))
                        index = input.IndexOf(repeat(i), index + 1)
                    End While
                Next
            End Sub

            Public Function hira2pron(ByRef input As String, Optional ByVal shortenlevel As Int32 = 2) As String
                Dim i As Int32
                Dim word As String = Nothing

                If Not input Is Nothing Then
                    If shortenlevel > 2 Then
                        shortenlevel = 2
                    ElseIf shortenlevel < 0 Then
                        shortenlevel = 0
                    End If
                    copy(word, input)
                    prepare(word)
                    For i = 0 To pron.GetLength(0) - 1
                        word = word.Replace(pron(i, 0), pron(i, 1))
                    Next

                    If shortenlevel > 0 Then
                        pronshorten(word, 1)
                    End If
                    If shortenlevel > 1 Then
                        pronshorten(word, 2)
                    End If
                    cleanseperator(word)
                    fixlongpron(word)
                End If
                Return word
            End Function

        End Module
    End Namespace
End Namespace
